/*
 * @Author: xuesong
 * @Date: 2021-11-30 10:44:34
 * @LastEditTime: 2021-12-04 09:44:46
 * @LastEditors: xuesong
 * @Description: 
 * @FilePath: \BLE\projects\ble_peripheral\uart_ble\src\TM_Driver\tm1628A.c
 */


#include "sdk_config.h"
#include "clock.h"
#include "log.h"
#include "OSAL.h"
#include "gpio.h"
#include "tm1628A.h"

#define  TM_POWER_PIN       GPIO_P31
#define  TM_DIO_PIN         GPIO_P32
#define  TM_CLK_PIN         GPIO_P33
#define  TM_STB_PIN         GPIO_P34

// uint16    TM_DataBuffer[TM1628_DISPLAY_SIZE];
DisplayBufferType Driver_DisplayBuff[Driver_DISPLAY_SIZE];

void Tm1628A_IO_PowerDown(void)
{
    HalGpioSet(TM_STB_PIN,Bit_DISABLE);
    HalGpioPinInit(TM_STB_PIN, GPIO_OUTPUT);   
    HalGpioPupdConfig(TM_STB_PIN,GPIO_PULL_DOWN);
    HalGpioSet(TM_DIO_PIN,Bit_DISABLE);
    HalGpioPinInit(TM_DIO_PIN, GPIO_OUTPUT);
    HalGpioPupdConfig(TM_DIO_PIN,GPIO_PULL_DOWN);
    HalGpioSet(TM_CLK_PIN,Bit_DISABLE);
    HalGpioPinInit(TM_CLK_PIN, GPIO_OUTPUT);   
    HalGpioPupdConfig(TM_CLK_PIN,GPIO_PULL_DOWN);    
    HalGpioPinInit(TM_POWER_PIN, GPIO_OUTPUT);   
    HalGpioSet(TM_POWER_PIN,Bit_DISABLE);
}

void Tm1628A_IO_Init(void)
{
    HalGpioSet(TM_STB_PIN,Bit_ENABLE);
    HalGpioPinInit(TM_STB_PIN, GPIO_OUTPUT);   
    HalGpioPupdConfig(TM_STB_PIN,GPIO_PULL_UP);
    HalGpioSet(TM_DIO_PIN,Bit_ENABLE);
    HalGpioPinInit(TM_DIO_PIN, GPIO_OUTPUT);
    HalGpioPupdConfig(TM_DIO_PIN,GPIO_PULL_UP_S);
    HalGpioSet(TM_CLK_PIN,Bit_ENABLE);
    HalGpioPinInit(TM_CLK_PIN, GPIO_OUTPUT);   
    HalGpioPupdConfig(TM_CLK_PIN,GPIO_PULL_UP_S);
}

void TM1628_Init(void)
{
    // uint16    TM_DataBuffer[TM1628_DISPLAY_SIZE];
    HalGpioSet(TM_POWER_PIN,Bit_ENABLE);
    HalGpioPinInit(TM_POWER_PIN, GPIO_OUTPUT);
    Tm1628A_IO_Init();
    WaitUs(10);
    // for(uint8 i = 0; i<TM1628_DISPLAY_SIZE; i++)
    //     TM_DataBuffer[i] = 0;
    // TM_Display(TM_DataBuffer,TM1628_DISPLAY_SIZE);
}

static void TM_Send8bit(uint8 data)
{
    for(uint8 i = 0; i < 8 ; i++){
        HalGpioSet(TM_CLK_PIN,Bit_DISABLE);     //CLK = 0
        if(data&0x01)
            HalGpioSet(TM_DIO_PIN,Bit_ENABLE);  //DIO = 1
        else
            HalGpioSet(TM_DIO_PIN,Bit_DISABLE); //DIO = 0
        WaitUs(1);
        HalGpioSet(TM_CLK_PIN,Bit_ENABLE);      //CLK = 1
        WaitUs(3);
        data >>= 1;
    }
    HalGpioSet(TM_CLK_PIN,Bit_DISABLE);         //CLK = 0
    // HalGpioSet(TM_DIO_PIN,Bit_DISABLE);         //DIO = 0
}

static void TM_SendCmd(uint8 cmd)
{
    HalGpioSet(TM_STB_PIN,Bit_ENABLE);          //STB = 1
    WaitUs(1);
    HalGpioSet(TM_STB_PIN,Bit_DISABLE);         //STB = 0
    TM_Send8bit(cmd);
    WaitUs(2);
}

void Data2TM1628(uint16 *dataBuff,uint8 len)
{
    TM_SendCmd(DISPLAY_MODE_CMD | DISPLAY_MODE_7C10);   //设置显示模式,7位10段
    TM_SendCmd(DISPLAY_DATA_CMD | DISPLAY_DATA_WRITE);  //设置显示命令,写数据自动加
    TM_SendCmd(DISPLAY_ADDR_CMD);                       //设置地址,0xC0
    if(len > Driver_DISPLAY_SIZE)
        len = Driver_DISPLAY_SIZE;
    for(uint8 i = 0; i<len ;i++)
    {
        TM_Send8bit((*dataBuff)&0xFF);
        TM_Send8bit(((*dataBuff)>>8)&0xFF);
        dataBuff++;
    }
    TM_SendCmd(DISPLAY_CONTROL_CMD|DISPLAY_CONTROL_ON|DISPLAY_CONTROL_PWM14_16);
    HalGpioSet(TM_STB_PIN,Bit_ENABLE);         //STB = 1
    // HalGpioSet(TM_CLK_PIN,Bit_ENABLE);         //CLK = 1
    HalGpioSet(TM_DIO_PIN,Bit_ENABLE);         //DIO = 1   
}

